package com.mhkj.fruit.key.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.mhkj.fruit.key.dao.KeyFruitArchivesDetailedDao;
import com.mhkj.fruit.key.entity.KeyFruitArchivesDetailed;
import com.mhkj.fruit.key.service.KeyFruitArchivesDetailedService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.system.base.entity.FileEntity;
import com.mics.core.system.base.service.FileService;
import com.mics.core.system.base.service.impl.BaseServiceImpl;
import com.mics.core.system.rbac.service.DictionaryService;

/**
 *  节点成果档案明细
 * @author wk
 * @date 2018年8月15日
 */
@Service
public class KeyFruitArchivesDetailedServiceImpl extends BaseServiceImpl<KeyFruitArchivesDetailed> implements KeyFruitArchivesDetailedService {
	@Resource
	private KeyFruitArchivesDetailedDao keyFruitArchivesDetailedDao;
	@Resource
	private DictionaryService dictionaryService;
	
	@Resource
	private FileService fileservice;

	@Resource
	public void setKeyFruitArchivesDetailedDao(KeyFruitArchivesDetailedDao keyFruitArchivesDetailedDao) {
		super.setBaseRepository(keyFruitArchivesDetailedDao);
	}

	@Override
	public Pagination<KeyFruitArchivesDetailed> listByPage(KeyFruitArchivesDetailed keyFruitArchivesDetailed, int pageIndex, int size) {
		Specification<KeyFruitArchivesDetailed> spec = new Specification<KeyFruitArchivesDetailed>() {
			/**
			 * 序列号
			 */
			private static final long serialVersionUID = -786003485941679959L;

			@Override
			public Predicate toPredicate(Root<KeyFruitArchivesDetailed> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];

				Join<Object, Object> keyFruitArchives = root.join("keyFruitArchives",JoinType.INNER);
				Join<Object, Object> planFeedBack = keyFruitArchives.join("planFeedBack",JoinType.INNER);
				if (!StringUtils.isEmpty(keyFruitArchivesDetailed.getKeyFruitArchives().getId())) {
					lstPredicates.add(criteriaBuilder.equal(keyFruitArchives.get("id").as(Long.class),keyFruitArchivesDetailed.getKeyFruitArchives().getId()));
				}
				if (!StringUtils.isEmpty(keyFruitArchivesDetailed.getKeyFruitArchives().getPlanFeedBack())) {
					lstPredicates.add(criteriaBuilder.equal(planFeedBack.get("id").as(Long.class),keyFruitArchivesDetailed.getKeyFruitArchives().getPlanFeedBack().getId()));
				}

				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		
		
		Page<KeyFruitArchivesDetailed> pageT = keyFruitArchivesDetailedDao.findAll(spec,PageRequest.of(pageIndex, size, Sort.Direction.ASC, "id"));
		Pagination<KeyFruitArchivesDetailed> pagination = new Pagination<KeyFruitArchivesDetailed>();
		pagination.setTotal(keyFruitArchivesDetailedDao.count(spec));
		pagination.setData(pageT.getContent());
		List<KeyFruitArchivesDetailed> list = pageT.getContent();
		for(KeyFruitArchivesDetailed keyFruitArchivesDetailed2 : list){

			List<FileEntity> listFileByTableIdAndName = fileservice.listFileByTableIdAndName(keyFruitArchivesDetailed2.getId(), "t_key_fruit_archives_detailed");
			keyFruitArchivesDetailed2.setListFile(listFileByTableIdAndName);
			
		}
		
		return pagination;
	}

}
